package com.xnyzc.lhy.finance.service.impl.payment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.finance.constant.BankCardConstant;
import com.xnyzc.lhy.finance.constant.BehalfConstant;
import com.xnyzc.lhy.finance.entity.payment.OaDBehalfPay;
import com.xnyzc.lhy.finance.entity.wallet.OaDWallet;
import com.xnyzc.lhy.finance.mapper.payment.OaDBehalfPayMapper;
import com.xnyzc.lhy.finance.mapper.wallet.OaDWalletMapper;
import com.xnyzc.lhy.finance.service.payment.IOaDBehalfPayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 司机钱包表 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-10-20
 */
@Slf4j
@Service
public class OaDBehalfPayServiceImpl extends ServiceImpl<OaDBehalfPayMapper, OaDBehalfPay> implements IOaDBehalfPayService {

    @Autowired
    private OaDWalletMapper oaDWalletMapper;

    @Autowired
    private OaDBehalfPayMapper oaDBehalfPayMapper;

    /**
     * 为司机垫付
     * @param driverId 司机ID
     * @param amount 垫付金额
     * @return Map
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Rv behalfDriverPayment(Long driverId, BigDecimal amount, Long orderNo, Long didiOrderId) {

        QueryWrapper<OaDWallet> oaDWalletQueryWrapper = new QueryWrapper<>();
        oaDWalletQueryWrapper.eq(OaDWallet.DRIVER_ID, driverId);
        oaDWalletQueryWrapper.select(OaDWallet.WALLET_ID, OaDWallet.CASH_BALANCE, OaDWallet.WALLET_BALANCE, OaDWallet.VERSION);

        OaDWallet oaDWallet = null;
        try {
            oaDWallet = oaDWalletMapper.selectOne(oaDWalletQueryWrapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw PangException.create(EErrorCode.walletException);
        }

        if (CheckUtil.objIsEmpty(oaDWallet)) {
            throw PangException.create(EErrorCode.unWalletInfo);
        }

        BigDecimal newWalletBalance = oaDWallet.getWalletBalance().add(amount);
        BigDecimal newCashBalance = oaDWallet.getCashBalance().add(amount);

        log.info("为司机垫付，原钱包余额为：{},原可提现余额：{}", oaDWallet.getWalletBalance(), oaDWallet.getCashBalance());

        oaDWallet.setWalletBalance(newWalletBalance);
        oaDWallet.setCashBalance(newCashBalance);

        log.info("为司机垫付,现钱包余额为：{},现可提现余额{}", newWalletBalance, newCashBalance);

        // 修改司机金额
        Integer updateResult = oaDWalletMapper.updateById(oaDWallet);

        Map<String, Integer> resultMap = new HashMap<String, Integer>(1);

        if (updateResult > 0) {

            Date date = new Date();

            OaDBehalfPay oaDBehalfPay = new OaDBehalfPay();
            oaDBehalfPay.setBehalfPayId(IDUtil.nextId());
            oaDBehalfPay.setOrderNo(orderNo);
            oaDBehalfPay.setDidiOrderId(didiOrderId);
            oaDBehalfPay.setDriverId(driverId);
            oaDBehalfPay.setBehalfPayAmount(amount);
            oaDBehalfPay.setTag(BankCardConstant.TAG);
            oaDBehalfPay.setCreateUser(driverId);
            oaDBehalfPay.setCreateTime(date);
            oaDBehalfPay.setUpdateUser(driverId);
            oaDBehalfPay.setUpdateTime(date);

            Integer insertResult = oaDBehalfPayMapper.insert(oaDBehalfPay);

            resultMap.put("isSuccess", insertResult);

            return Rv.wrap(resultMap);
        }

        throw PangException.create(EErrorCode.sysError);
    }

    /**
     * 司机垫付回款
     * @param driverId 司机ID
     * @return Map
     */
    @Override
    public Rv driverPaid(Long driverId, Long orderNo, Long didiOrderId) {

        QueryWrapper<OaDBehalfPay> oaDBehalfPayQueryWrapper = new QueryWrapper<>();
        oaDBehalfPayQueryWrapper.eq(OaDBehalfPay.DRIVER_ID, driverId);
        oaDBehalfPayQueryWrapper.eq(OaDBehalfPay.ORDER_NO, orderNo);
        oaDBehalfPayQueryWrapper.eq(OaDBehalfPay.DIDI_ORDER_ID, didiOrderId);
        oaDBehalfPayQueryWrapper.eq(OaDBehalfPay.BEHALF_PAY_STATUS, BehalfConstant.BEHALF_NOT_PAYMENT);
        oaDBehalfPayQueryWrapper.select(OaDBehalfPay.BEHALF_PAY_ID, OaDBehalfPay.VERSION);

        OaDBehalfPay oaDBehalfPay = oaDBehalfPayMapper.selectOne(oaDBehalfPayQueryWrapper);

        if (CheckUtil.objIsEmpty(oaDBehalfPay)) {
            throw PangException.create(EErrorCode.notFoundBehalfPayInfo);
        }

        OaDBehalfPay updateOaDBehalfPay = new OaDBehalfPay();
        updateOaDBehalfPay.setBehalfPayId(oaDBehalfPay.getBehalfPayId());
        updateOaDBehalfPay.setBehalfPayStatus(BehalfConstant.BEHALF_PAID);
        updateOaDBehalfPay.setVersion(oaDBehalfPay.getVersion());

        Integer updateResult = oaDBehalfPayMapper.updateById(updateOaDBehalfPay);

        Map<String, Integer> resultMap = new HashMap<>(1);

        resultMap.put("isSuccess", updateResult);

        return Rv.wrap(resultMap);
    }
}
